<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue监测数据改变的原理_数组</title>
    <!-- 引入Vue -->
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <!--
        
    -->
    <!-- 准备好一个容器 -->
    <div id="root">
        <h2>学校名称：{{school.name}}</h2>
        <h2 v-if="school.leader">学校校长：{{school.leader}}</h2>
        <h2>学校地址：{{school.address}}</h2>
        <button @click="addSex">添加属性</button>
        <hr/>
        <h2>姓名：{{student.name}}</h2>
        <h2 v-if="student.sex">性别：{{student.sex}}</h2>
        <h2>年龄：真实{{student.age.rAge}}，对外{{student.age.sAge}}</h2>
        <h2>爱好</h2>
        <ul>
            <li v-for="(h, index) in student.hobby" :key="index">
                {{h}}
            </li>
        </ul>
        <h2>朋友们</h2>
        <ul>
            <li v-for="(item, index) in student.friends" :key="index">
                {{item.name}}--{{item.age}}
            </li>
        </ul>
    </div>
</body>

<script type="text/javascript">
    Vue.config.productionTip = false;   // 阻止Vue在启动时生成生成提示

    const vm = new Vue({
        el: "#root",
        data: {
            school: {
                name: "尚硅谷",
                address: "北京",
            },
            student: {
                name: "Tom",
                age: {
                    rAge: 40,
                    sAge: 28
                },
                hobby: ['抽烟', '喝酒', '烫头'],
                friends: [
                    {name: 'jerry', age: 35},
                    {name: 'tony', age: 36}
                ]
            }
        },
        methods: {
            addSex() {
                console.log(Vue, this);
                /* 
                    不要强行使用 this.student.sex = "男"
                    无效
                    不会生成对应的 get函数和set函数
                */
                // Vue.set(this.student, "sex", "男");
                this.$set(this.student, "sex", "男");

                /* 
                    不允许直接加在Vue实例身上
                    this.$set(this, "leader", "张三丰");
                */
                this.$set(this.school, "leader", "张三丰");
                
            }
        }
    })

    
</script>
</html>